/*
 * ROMEFeedImpl.java
 *
 * Created on August 13, 2007, 1:17 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package com.afaker.rss.feed;

import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndImage;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.xml.sax.InputSource;

/**
 *
 * @author bruce
 */
public class ROMEFeedImpl implements Feed {

    private SyndFeed feed;
    private SyndFeedInput input = new SyndFeedInput();
    private InputSource source;
    private EntrySet entries;
    ;

    /** Creates a new instance of ROMEFeedImpl */
    public ROMEFeedImpl(SyndFeed feed) {
        this.feed = feed;
    }

    public ROMEFeedImpl() {
    }

    public SyndFeed build(String uri) throws FeedException, FileNotFoundException {
        File file;
        if (uri.startsWith("http://")) {
            source = new InputSource(uri);
            feed = input.build(source);
        } else {
            file = new File(uri);
            try {
                feed = input.build(file);
            } catch (IllegalArgumentException ex) {
                ex.printStackTrace();
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (FeedException ex) {
                ex.printStackTrace();
            }
        }
        return feed;
    }

    public String getTitle() {
        return feed.getTitle().trim();
    }

    public String getAuthor() {
        return feed.getAuthor();
    }

    public String getDescription() {
        return feed.getDescription();
    }

    public String getFeedType() {
        return feed.getFeedType();
    }

    public String getLink() {
        return feed.getLink();
    }

    public List getCategories() {
        return feed.getCategories();
    }

    public EntrySet getEntries() {
        if (entries == null) {
            setEntrySet();
        }
        return entries;
    }

    public SyndImage getImage() {
        return feed.getImage();
    }

    public String getURI() {
        return feed.getUri();
    }

    public void removeEntry(String uri) {
        if (entries == null) {
            setEntrySet();
        }
        entries.removeEntry(uri);
    }

    public void addEntry(FeedEntry entry) {
        entries.addEntry(entry);
    }

    private void setEntrySet() {
        entries = new EntrySet();
        Iterator it = feed.getEntries().listIterator();
        ROMEFeedEntryImpl entity = null;
        SyndEntry temp;
        while (it.hasNext()) {
            temp = (SyndEntry) it.next();
            entity = new ROMEFeedEntryImpl(temp, getTitle());
            entries.addEntry(entity);
        }
    }

    public SyndFeed getFeed() {
        return feed;
    }
}
